package com.cn.zpark.servlet;

import com.cn.zpark.beans.User;
import com.cn.zpark.utils.DruidUtils;
import com.cn.zpark.utils.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @PackageName: com.cn.zpark.servlet
 * @ClassName: PageFindUserServlet
 * @Description:
 * @author: RZS
 * @date: 2022/4/22  20:23
 */
@WebServlet("/page_find_user.do")
public class PageFindUserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 准备连接信息
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet res = null;

        List<User> lists = new ArrayList<>();
        PageInfo<User> pageInfo = new PageInfo<>();

        // 获取请求参数pageInfo
        String page = request.getParameter("pageInfo");
        // 判断pageInfo是否有数据，如果没有，则为第一次访问，分页信息为默认
        if(page == null || "null".equals(page)){
            // 则为第一次访问，分页信息为默认

            try {
                // 获取连接对象
                conn = DruidUtils.getConn();
                // 编写sql
                String sql = "select id, username, password, phone, email, salary from user limit 0, ?";
                // 创建预编译sql
                stat = conn.prepareStatement(sql);
                // 替换占位符
                stat.setInt(1, pageInfo.getNumber());
                // 执行sql，并且获取结果集
                res = stat.executeQuery();
                while (res.next()) {
                    User user = new User();
                    user.setId(res.getInt("id"));
                    user.setUsername(res.getString("username"));
                    user.setPassword(res.getString("password"));
                    user.setPhone(res.getString("phone"));
                    user.setEmail(res.getString("email"));
                    user.setSalary(res.getDouble("salary"));

                    // 添加到集合
                    lists.add(user);
                }

                // 封装数据到pageInfo对象中
                pageInfo.setInfo(lists);

                // 查询总页数
                sql = "select count(*) num from user";
                stat = conn.prepareStatement(sql);
                res = stat.executeQuery();
                int sumNumber = 0;
                while (res.next()) {
                    sumNumber = res.getInt(1);
                }

                // 设置总条数
                pageInfo.setSumNumber(sumNumber);
                // 设置当前页码
                pageInfo.setCurrentPage(0);
                System.out.println(pageInfo);


                // 将pageInfo对象绑定到请求域对象
                request.setAttribute("pageData", pageInfo);
                // 转发给JSP做处理
                request.getRequestDispatcher("jsp/demo01.jsp").forward(request, response);
            } catch(Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭资源
                DruidUtils.close(conn, res, stat);
            }


        }else{
            // 当不是第一次访问时，获取用户分页请求数据
            // 获取总页数
            Integer pageSum = Integer.parseInt(request.getParameter("pageSum"));
            // 获取总数
            Integer sum = Integer.parseInt(request.getParameter("sum"));
            // 获取访问页码
            Integer pageCode = Integer.parseInt(request.getParameter("pageCode"));
            // 获取每页显示数量
            Integer number = Integer.parseInt(request.getParameter("number"));

            // 查询分页
            try {
                // 获取连接对象
                conn = DruidUtils.getConn();
                // 编写sql
                String sql = "select id, username, password, phone, email, salary from user limit ?, ?";
                // 创建预编译sql
                stat = conn.prepareStatement(sql);
                // 替换占位符
                stat.setInt(1, pageCode * number);
                stat.setInt(2, pageInfo.getNumber());
                // 执行sql，并且获取结果集
                res = stat.executeQuery();
                while (res.next()) {
                    User user = new User();
                    user.setId(res.getInt("id"));
                    user.setUsername(res.getString("username"));
                    user.setPassword(res.getString("password"));
                    user.setPhone(res.getString("phone"));
                    user.setEmail(res.getString("email"));
                    user.setSalary(res.getDouble("salary"));

                    // 添加到集合
                    lists.add(user);
                }

                // 封装数据到pageInfo对象中
                pageInfo.setInfo(lists);


                // 设置总条数
                pageInfo.setSumNumber(sum);
                // 设置当前页码
                pageInfo.setCurrentPage(pageCode);
                // 设置总页数
                pageInfo.setPageNumber(pageSum);
                // 设置每一页查询的条数
               pageInfo.setNumber(number);


                // 将pageInfo对象绑定到请求域对象
                request.setAttribute("pageData", pageInfo);
                // 转发给JSP做处理
                request.getRequestDispatcher("jsp/demo01.jsp").forward(request, response);
            } catch(Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭资源
                DruidUtils.close(conn, res, stat);
            }

        }
    }
}
